var express = require('express');
var logger = require('morgan');
var app = express();
var mongoose = require('mongoose');
var KLPS = require('./index.js');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var path = require('path');

var jwt = {
    header: {
        "typ": "JWT",
        "alg": "sha256"
    },
    payload: {
        "iss": "ooorange.com",
        "exp": new Date().getTime() + 1800000,
        "uid": "C02EDF62D06BA206ADAF02B6006C1507",
    },
    SecrectKey: "1F49703E42686587094880736CCB1D9E"
};
app.jwt = jwt;

app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
    extended: false
}));


app.use(cookieParser());

var klps = new KLPS(app);
// 连接数据库
var uri = 'mongodb://127.0.0.1/klps';
mongoose.connect(uri, {
    useMongoClient: true
}, function(err) {
    if (!err) {
        console.log('connect db success')
    }
});
mongoose.connection.on('error', console.error.bind(console, '连接错误:'));
app.set('views', path.resolve(__dirname, './views/'))
app.set("view engine", "ejs");




app.get('/', klps.authentication, function(req, res, next) {
    res.render('home');
});
app.get('/login', function(req, res, next) {

    res.render('login');
});
app.post('/doLogin', klps.login, function(req, res, next) {
    console.log(res.cookie)
    res.render('home')
})

app.get('/logout', klps.logout, function(req, res, next) {
    res.render('login')
})
app.get('/register', function(req, res, next) {
    res.render('register')
})
app.post('/doRegister', klps.register, function(req, res, next) {
    res.status(200).send("success")
})
module.exports = app;
app.listen(8000);